package folder;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This Servlet creates a List with all the content of a Folder. If no Folder is
 * given, it takes the main Folder: pics
 * 
 * 
 * @author cuche
 * @date 21.05.2014
 * 
 * 
 */
public class FileListServlet extends HttpServlet {
	private static final String SAVE_DIR_PATH = "/pics";
	private static final String URL_TO_GALARY = "/images.jsp";
	private static final String DIR_PARAMETER = "dirPath";
	private static final long serialVersionUID = 1L;

	public FileListServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handleRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handleRequest(request, response);
	}

	/**
	 * This Method is used for handling both the doGet() and doPost() methods,
	 * the code would better in the doPost but since I hadn't much time left, I
	 * decided to take it for both possibilites. But it would be clearly better
	 * with doPost.
	 * 
	 * @param request
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 * @throws ServletException
	 */
	private void handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws IllegalStateException,
			IOException, ServletException {
		ServletContext context = getServletContext();
		String pathToDir = getPathToDir(request, context);
		File directory = new File(pathToDir);
		Set<FileBean> filesInDir = createListOfFilesInDir(directory, context);

		RequestDispatcher rd = context.getRequestDispatcher(URL_TO_GALARY);
		request.setAttribute("folderContent", filesInDir);
		request.setAttribute("currentDir", pathToDir);

		if (isNotSaveDir(pathToDir, context)) {
			request.setAttribute("superDir", getPathToSuperDir(directory));
		} else {
			request.setAttribute("superDir", null);
		}
		rd.forward(request, response);
	}

	/**
	 * 
	 * @param request
	 * @param context
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 * @throws ServletException
	 */
	private String getPathToDir(HttpServletRequest request,
			ServletContext context) throws IllegalStateException, IOException,
			ServletException {
		String pathFromRequest = request.getParameter(DIR_PARAMETER);
		if (pathFromRequest != null) {
			return pathFromRequest;
		} else {
			return context.getRealPath(SAVE_DIR_PATH);
		}
	}

	/**
	 * This method creates a List of Files that are in a directory
	 * 
	 * @param directory
	 * @param context
	 * @return list
	 */

	private Set<FileBean> createListOfFilesInDir(File directory,
			ServletContext context) {

		File[] filesInDir = directory.listFiles();
		Set<FileBean> fileSet = new HashSet<FileBean>();
		if (filesInDir != null && filesInDir.length > 0) {
			for (File file : filesInDir) {
				fileSet.add(createFileBean(file));
			}
		}

		return fileSet;
	}

	/**
	 * Creates a FileBean
	 * 
	 * @param file
	 * @return all information needed for the servlets
	 */
	private FileBean createFileBean(File file) {
		FileBean fileBean = new FileBean();
		fileBean.setFileName(file.getName());
		fileBean.setPath(file.getPath());
		fileBean.setDirectory(file.isDirectory());
		return fileBean;
	}

	/**
	 * 
	 * @param pathToDirToDisplay
	 * @param context
	 * @return path
	 */
	private boolean isNotSaveDir(String pathToDirToDisplay,
			ServletContext context) {
		String pathToSaveDir = context.getRealPath(SAVE_DIR_PATH);
		return !pathToSaveDir.equals(pathToDirToDisplay);
	}

	/**
	 * This method serves the Link for the super directory and return the parent
	 * 
	 * @param directory
	 * @return parent directory
	 */
	private String getPathToSuperDir(File directory) {
		return directory.getParentFile().getPath();
	}

}// END OF CLASS
